package my.download.util;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.*;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Iterator;

public class ExcelUtils {

    public static <T> void exportExcel2007(String title, String[][] headers, Collection<T> dataset, OutputStream out)
            throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // 声明一个工作薄
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 生成一个表格
        XSSFSheet sheet = workbook.createSheet(title);
        // 设置表格默认列宽度为15个字节
        sheet.setDefaultColumnWidth(20);
        // 生成表头样式
        XSSFCellStyle headerStyle = workbook.createCellStyle();
        // 设置这些样式
        headerStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headerStyle.setBorderBottom(BorderStyle.THIN);
        headerStyle.setBorderLeft(BorderStyle.THIN);
        headerStyle.setBorderRight(BorderStyle.THIN);
        headerStyle.setBorderTop(BorderStyle.THIN);
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        // 生成一个字体
        XSSFFont headerFont = workbook.createFont();
        headerFont.setFontName("宋体");
        headerFont.setColor(IndexedColors.WHITE.getIndex());
        headerFont.setFontHeightInPoints((short) 11);
        // 把字体应用到当前的样式
        headerStyle.setFont(headerFont);
        // 产生表格标题行
        XSSFRow headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            String[] header = headers[i];
            XSSFCell cell = headerRow.createCell(i);
            cell.setCellStyle(headerStyle);
            cell.setCellValue(new XSSFRichTextString(header[0]));
        }

        // 生成并设置表格样式
        XSSFCellStyle bodyStyle = workbook.createCellStyle();
        bodyStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        bodyStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        bodyStyle.setBorderBottom(BorderStyle.THIN);
        bodyStyle.setBorderLeft(BorderStyle.THIN);
        bodyStyle.setBorderRight(BorderStyle.THIN);
        bodyStyle.setBorderTop(BorderStyle.THIN);
        bodyStyle.setAlignment(HorizontalAlignment.CENTER);

        // 遍历集合数据，产生数据行
        Iterator<T> it = dataset.iterator();
        for (int index = 0; it.hasNext();) {
            XSSFRow row = sheet.createRow(++index);
            T t = it.next();
            // 利用反射，根据JavaBean属性的先后顺序，动态调用getXxx()方法得到属性值
            for (int i = 0; i < headers.length; i++) {
                XSSFCell cell = row.createCell(i);
                String column = headers[i][1];
                cell.setCellStyle(bodyStyle);
                String getMethodName = "get" + column.substring(0, 1).toUpperCase() + column.substring(1);
                Class<?> tCls = t.getClass();
                Method getMethod = tCls.getMethod(getMethodName);
                Object value = getMethod.invoke(t);
                String textValue = null;
                if (value instanceof Boolean) {
                    textValue = "是";
                    if (!(Boolean) value) {
                        textValue = "否";
                    }
                } else if (value instanceof LocalDate) {
                    textValue = DateUtils.formatLocalDate((LocalDate) value);
                } else if (value instanceof LocalDateTime) {
                    textValue = DateUtils.formatLocalDateTime((LocalDateTime) value);
                }else {
                    if (value != null) {
                        textValue = value.toString();
                    }
                }
                if (textValue != null) {
                    cell.setCellValue(textValue);
                }
            }
        }
        workbook.write(out);
    }
}
